package com.yuanduan.mplus.view;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.cj.framework.app.BaseActivity;
import com.cj.framework.util.HttpImp;
import com.cj.framework.util.IHttp;
import com.cj.framework.util.RSAUtils;
import com.yuanduan.mplus.R;
import com.yuanduan.mplus.config.Config;
import com.yuanduan.mplus.model.DBManager;
import com.yuanduan.mplus.model.entity.DirverEntity;
import com.yuanduan.mplus.model.entity.DriveAnalyseEntity;
import com.yuanduan.mplus.model.entity.User;
import com.yuanduan.mplus.presenter.DriveAnalyseAdapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class DriveAnalyseActivity extends BaseActivity implements AdapterView.OnItemClickListener {
    private Toolbar toolbar;
    private ListView listView;

    private User user;
    private ArrayList<DirverEntity> drivers=new ArrayList<>();

    DriveAnalyseAdapter driveAnalyseAdapter;
    private IHttp iHttp=new HttpImp();
    private IHttp.HttpInfoCallback callback=new IHttp.HttpInfoCallback() {
        @Override
        public void onSucceed(Object result) {
            String result1= RSAUtils.decryptWithRSA((String) result);
            List<DriveAnalyseEntity> lists=new ArrayList<>();
            try {
                JSONObject data=new JSONObject(result1);
                if(data==null){
                    return;
                }
                if(data.getInt("RS")!=1){
                    return;
                }
                JSONArray jsonArray=data.getJSONArray("Driver");
                for(int i=0;i<jsonArray.length();i++){
                    JSONObject jsonObject=jsonArray.getJSONObject(i);
                    DriveAnalyseEntity entity=new DriveAnalyseEntity();
                    entity.setUserPhone(jsonObject.getString("UserPhone"));
                    entity.setDistance(jsonObject.getString("DrivingMileage"));
                    entity.setTime(jsonObject.getString("DrivingTimer"));
                    entity.setSafeScore(jsonObject.getInt("Security"));
                    entity.setEconomicalScore(jsonObject.getInt("Economic"));
                    entity.setLevel(jsonObject.getInt("Evaluation"));
                    lists.add(entity);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            match(lists);
            driveAnalyseAdapter.setDatas(lists);
        }

        @Override
        public void onFailed(Exception e) {
            Toast.makeText(DriveAnalyseActivity.this,"获取失败",Toast.LENGTH_SHORT).show();
        }
    };

    private void match(List<DriveAnalyseEntity> lists) {
        for(int i=0;i<lists.size();i++){
            String userPhone=lists.get(i).getUserPhone();
            for(int j=0;j<drivers.size();j++){
                if(userPhone.equals(drivers.get(j).getUserPhone())){
                    lists.get(i).setUserName(drivers.get(j).getRealName());
                    break;
                }
            }
        }
    }

    @Override
    protected void findId() {
        toolbar= (Toolbar) findViewById(R.id.toolbar);
        listView= (ListView) findViewById(R.id.driveanalyse_listview);
    }

    @Override
    protected void logic() {
        user=DBManager.getInstance(getApplicationContext()).getUser();
        //设置toolbar
        toolbar.setTitle("车队驾驶行为分析");
        toolbar.setTitleTextColor(Color.BLACK);
        toolbar.setNavigationIcon(R.mipmap.back_arrow_black);
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DriveAnalyseActivity.this.finish();
            }
        });

        driveAnalyseAdapter=new DriveAnalyseAdapter(this);
        listView.setAdapter(driveAnalyseAdapter);
        listView.setOnItemClickListener(this);
        HashMap<String,String> hashMap1=new HashMap<>();
        hashMap1.put("UserPhone",user.getUserPhone());
        hashMap1.put("UserPassword",user.getUserPwd());
        hashMap1.put("CarAdminPhone",user.getUserPhone());
        iHttp.httpInfoPost(Config.URL + Config.DRIVERLISTOFCARADMIN, Config.translateMap(hashMap1), new IHttp.HttpInfoCallback() {
            @Override
            public void onSucceed(Object result) {
                String result1=RSAUtils.decryptWithRSA((String) result);
                try {
                    JSONObject jsonObject=new JSONObject(result1);
                    if(jsonObject==null){
                        return;
                    }
                    if(jsonObject.getInt("RS")!=1){
                        return;
                    }
                    JSONArray jsonArray=jsonObject.getJSONArray("Driver");
                    for(int i=0;i<jsonArray.length();i++){
                        JSONObject object=jsonArray.getJSONObject(i);
                        DirverEntity entity=new DirverEntity();
                        entity.setUserPhone(object.getString("UserPhone"));
                        entity.setUserID(object.getInt("UserID"));
                        entity.setRealName(object.getString("RealName"));
                        drivers.add(entity);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                HashMap<String,String> hashMap=new HashMap<>();
                hashMap.put("UserPhone", user.getUserPhone());
                iHttp.httpInfoPost(Config.URL+Config.DRIVINGINFORMATION,Config.translateMap(hashMap),callback);
            }

            @Override
            public void onFailed(Exception e) {

            }
        });
    }

    @Override
    protected int setLayout() {
        return R.layout.activity_drive_analyse;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Bundle bundle=new Bundle();
        bundle.putString("UserPhone",((DriveAnalyseEntity)driveAnalyseAdapter.getItem(position)).getUserPhone());
        bundle.putString("RealName",((DriveAnalyseEntity)driveAnalyseAdapter.getItem(position)).getUserName());
        openActivity(DriveScoreActivity.class,bundle);
    }
}
